﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="common/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
    	<title>执行Job</title>
<jsp:include page="common/header.jsp"/>
		<script type="text/javascript">
			var paraTypeName = new Object();
			
			function addDependency(jid) {
				if (typeof jid != "string") jid = $("select[name=_job]").val();
				$.ajax({
					url: "ajax.action",
					async: false,
					data: "identifier=" + jid,
					dataType: "json",
					success: function(data){
						var line = $("#dependencyLine").clone().removeAttr("id");
						$(":first", line).text($("input[name=dependencyJob]").size()+1);
						$(":eq(1)", line).text(data.obj.identifier.stringIdentifier);
						$(":eq(2)", line).text(data.obj.type);
						$(":eq(3)", line).text(data.obj.name);
						$(":eq(4)", line).text(data.obj.description);
						$(":eq(7)", line).html(getConditionParameter(data.obj));
						$(":eq(6)", line).html(getJobParameter(data.obj));
						$(":eq(5)", line).html(getKeyParameter());
						$(line).find(":image[name=removeDependency]").click(function(){
							line.remove();
							changeDependencyJob();
							$("input[name=dependencyJob]").each(function(index){
								$(":first", $(this).parent().parent()).text(index+1);
							});
							return false;
						});
						$("#dependencies").append(line);
						line.show();
						changeDependencyJob();
					}
				});
				return false;
			}
			
			function changeDependencyJob() {
			}
			
			function getKeyParameter() {
				var txt = '';
				$("input[name=dependKeys]:checked").each(function(index){
					if (index>0) txt += '<BR>\n';
					txt += '<b>' + $(this).val() + '</b>=<input type=text name=dependencyKeyPara><span class="highlight">*</span>';
				});
				return txt;
			}
			
			function getJobParameter(conf) {
				var txt = '<input type=hidden name=dependencyJob value="' + conf.identifier.stringIdentifier + '">';
				for (var i=0,size=conf.parameterSize;i<size;i++) {
					var p = conf.parameters[i];
					if (i>0) txt += '<BR>\n';
					txt += '<b>' + p.name + '</b>，' + paraTypeName[p.type];
					if (p.type != 'none') {
						txt += '：' + p.value;
					}
				}
				return txt;
			}

			function getConditionParameter(conf) {
				return '<input type=' + (conf.parameterSize>0?'text':'hidden') + ' name=dependencyJobPara>';
			}
			function createTabBar(htmlDiv1) {
				var tabbar = new dhtmlXTabBar(htmlDiv1, "top");
				tabbar.setSkin('dhx_skyblue');
				tabbar.setImagePath("static/dhtmlx/imgs/");
				<c:set var="tabIndex" value="${0}"/>
				<c:if test="${fn:length(parentConfs)>0}"><c:set var="tabIndex" value="${tabIndex+1}"/>tabbar.addTab("a${tabIndex}", "被以下任务引用", "100px");tabbar.setContent("a${tabIndex}", "refer");</c:if>
				<c:if test="${fn:length(fireConfs)>0}"><c:set var="tabIndex" value="${tabIndex+1}"/>tabbar.addTab("a${tabIndex}", "可触发以下任务", "100px");tabbar.setContent("a${tabIndex}", "refer-dependency");</c:if>
				<c:if test="${fn:length(triggers)>0}"><c:set var="tabIndex" value="${tabIndex+1}"/>tabbar.addTab("a${tabIndex}", "调度信息", "100px");tabbar.setContent("a${tabIndex}", "scheduler");</c:if>
				<c:if test="${fn:length(dependencies)>0}"><c:set var="tabIndex" value="${tabIndex+1}"/>tabbar.addTab("a${tabIndex}", "可被以下任务触发", "100px");tabbar.setContent("a${tabIndex}", "dependency");</c:if>
				tabbar.enableAutoSize(true, true);
			<c:choose>
				<c:when test="${tabIndex>0}">tabbar.setTabActive("a${tabIndex}");</c:when>
				<c:otherwise>$("#TabBarDiv").hide();</c:otherwise>
			</c:choose>
			}
			
			$(function(){
				$('input[name=execute]:radio').click(function(){
					$('.trigger').hide();
					$('.' + $(this).val()).show();
				});
				$('input[name=execute][value=${execute==null?"now":execute}]:radio').prop("checked", "checked").click();
				
				//依赖相关
				$('input[name=dependKeys]:checkbox').click(function(){
					var txt = getKeyParameter();
					$("input[name=dependencyJob]").each(function(index){
						$(":eq(5)", $(this).parent().parent()).html(txt);
					});
					if ($("input[name=dependKeys]:checked").size()>0) {
						$(".fsp").show();
					} else {
						$(".fsp").hide();
					};
				});
				
				<c:forEach var="pd" items="${defineTypes}">
					paraTypeName.${pd} = "${ss:getParameterDefineTypeName(pd)}";
				</c:forEach>
				$("#dependencyLine").hide();
				$(":image[name=addDependency]").click(addDependency);
				if ($("input[name=dependKeys]:checked").size()>0) {
					$(".fsp").show();
				} else {
					$(".fsp").hide();
				};
				<c:forEach var="j" items="${dependencyJob}">
					addDependency("${j.stringIdentifier}");
				</c:forEach>
				//设置关键参数
				var dependencyKeyPara = new Object();
				<c:forEach var="p" items="${dependencyKeyPara}" varStatus="vs">
					dependencyKeyPara[${vs.index}] = "${fn:replace(p,'\"', '\\"')}";
				</c:forEach>
				$("input[name=dependencyKeyPara]").each(function(idx, ele){
					$(this).val(dependencyKeyPara[idx]);
				});
				//设置依赖Job参数
				var dependencyJobPara = new Object();
				<c:forEach var="p" items="${dependencyJobPara}" varStatus="vs">
					dependencyJobPara[${vs.index}] = "${fn:replace(p,'\"', '\\"')}";
				</c:forEach>
				$("input[name=dependencyJobPara]").each(function(idx, ele){
					$(this).val(dependencyJobPara[idx]);
				});
				changeDependencyJob();

				createTabBar('TabBarDiv');
			});
		</script>
	</head>
	<body>
<jsp:include page="common/body-header.jsp"/>
		<form action="run-job-conf.action" method="post" class="yform">
<jsp:include page="fragment/job-conf-view-base.jsp"/>
<c:if test='${conf.parameterSize>0 || conf.type == "cmd"}'>
		<hr>
		<b>参数信息</b>
		<table>
	<c:if test='${conf.parameterSize>0}'>
			<tr valign="top">
				<td align="right" width="100">参数：</td>
				<td><ol><c:forEach var="p" items="${conf.parameters}" varStatus="vs">
					<li>参数名：<b>${p.name}</b>，当前值：<input type="text" name="paraValue" value="${paraValue[vs.index]}"><c:if test="${p.type=='none'}"><span class="highlight">*</span></c:if>，预设值：${ss:getParameterDefineTypeName(p.type)}<c:if test="${p.type!='none'}">，内容：${p.value}</c:if></li>
				</c:forEach></ol></td>
			</tr>
	</c:if>
	<c:if test='${conf.type == "cmd"}'>
			<tr valign="top">
				<td align="right" width="100">服务器资源：</td>
				<td>${conf.resource}</td>
			</tr>
			<tr valign="top">
				<td align="right" width="100">重试次数：</td>
				<td>${conf.maxRetry}</td>
			</tr>
	</c:if>
		</table>
</c:if>
		<hr>
		<b>执行方式</b>
		<table>
			<tr>
				<td align="right" width="100">执行方式：</td>
				<td>
					<input type="radio" name="execute" value="now">立即执行
					<input type="radio" name="execute" value="simple">简单调度
					<input type="radio" name="execute" value="cron">Cron调度
					<input type="radio" name="execute" value="depend">依赖触发
				</td>
			</tr>
			<tr valign="top" class="trigger simple cron depend">
				<td align="right">描述：</td>
				<td><textarea name="triggerDesc" cols="80">${triggerDesc}</textarea></td>
			</tr>
			<tr valign="top" class="trigger simple cron">
				<td align="right">开始时间：</td>
				<td><input type="text" name="startAt" value="${startAt}">(格式：yyyy-MM-dd HH:mm:ss)</td>
			</tr>
			<tr valign="top" class="trigger simple cron">
				<td align="right">结束时间：</td>
				<td><input type="text" name="endAt" value="${endAt}">(格式：yyyy-MM-dd HH:mm:ss)</td>
			</tr>
			<tr valign="top" class="trigger simple">
				<td align="right">执行次数：</td>
				<td><input type="text" name="repeatCount" value="${repeatCount}">(至少执行1次，不填为不限次数)</td>
			</tr>
			<tr valign="top" class="trigger simple">
				<td align="right">重复间隔：</td>
				<td><input type="text" name="interval" value="${interval}"><span class="highlight">*</span>(单位：秒)</td>
			</tr>
			<tr valign="top" class="trigger cron">
				<td align="right">Cron表达式：</td>
				<td><input type="text" name="cron" value="${cron}"><span class="highlight">*</span>(格式：Seconds(0-59) Minutes(0-59) Hours(0-23) Day-of-month(1-31) Month(1-12) Day-of-Week(1-7) Year(1970-2099,Optional))</td>
			</tr>
	<c:if test="${conf.parameterSize>0}">
			<tr valign="top" class="trigger depend">
				<td align="right">关键参数：</td>
				<td><c:forEach var="p" items="${conf.parameters}" varStatus="vs">
					<input type="checkbox" name="dependKeys" value="${p.name}"<c:if test="${fn:indexOf(fn:join(dependKeys, ','), p.name)>=0}"> checked="checked"</c:if>><b>${p.name}</b>
				</c:forEach></td>
			</tr>
	</c:if>
			<tr valign="top" class="trigger depend">
				<td align="right">依赖项：</td>
				<td><table id="dependencies">
					<tr>
						<th>No.</th>
						<th>Job Configuration Id</th>
						<th>Type</th>
						<th>Name</th>
						<th>Description</th>
						<th class="fsp">Key Conditions</th>
						<th>Parameters</th>
						<th>Condition</th>
						<th>&nbsp;</th>
					</tr>
					<tr id="dependencyLine">
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td class="fsp">&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>
							<input type="image" name="removeDependency" value="Remove" src="images/icons/remove.jpg" title="删除依赖项">
						</td>
					</tr>
				</table>
				<select name="_job"><c:forEach var="c" items="${confs}">
					<option value="${c.identifier.stringIdentifier}">${c.identifier.stringIdentifier}:${c.name}:${c.description}</option>
				</c:forEach></select>
				<input type="image" name="addDependency" value="Add" src="images/icons/add.png" title="添加新的依赖项"></td>
			</tr>
		</table>
		<hr>
			<input type="hidden" name="identifier" value="${conf.identifier.stringIdentifier}">
			<input type="hidden" name="action" value="execute">
			<c:if test="${message!=null}"><div style="color: red;">${message}</div></c:if>
			<input type="submit" value="执行">
		</form>
		<hr>
		<div id="TabBarDiv" style="width:100%; height:90px;"></div>
<jsp:include page="fragment/job-conf-view-refer.jsp"/>
<jsp:include page="fragment/job-conf-view-refer-dependency.jsp"/>
<jsp:include page="fragment/job-conf-view-scheduler.jsp"/>
<jsp:include page="fragment/job-conf-view-dependency.jsp"/>
<jsp:include page="common/body-footer.jsp"/>
	</body>
</html>
